package com.qgs.rabbitmqapi.ack;

import com.qgs.rabbitmqapi.consumer.MyConsumer;
import com.qgs.rabbitmqapi.consumer.MyHeaderConsumer;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: 蔡翔
 * @Date: 2019/7/19 13:26
 * @Version 1.0
 */
public class Consumer {
    public static void main(String[] args) throws Exception {
        Logger logger = LoggerFactory.getLogger(Consumer.class);

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.0.229");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPort(5672);

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        String exchange = "test_ack_exchange";
        String routingKey = "ack.#";
        String queueName = "test_ack_queue";

        channel.queueDeclare(queueName,true,false,false,null);
        channel.exchangeDeclare(exchange,"topic",true);
        channel.queueBind(queueName,exchange,routingKey);

        //1.限流方式，记得autoAck 必须设置为false，不然BasicQos两个参数是不会起效的。
        //channel.basicQos(0,1,false);//参数1,3 固定为 0和false。设置为false为Consumer级别不是channel级别。
        // 中间那个10 表示是我先接受10条，当我这10条处理完后，发给你Ack消息了，你在发我数据。
        //prefetchSize:0 表示不限制
        channel.basicConsume(queueName,false,new MyHeaderConsumer(channel));
    }
}
